package com.bs.controller;

import com.bs.bean.BillType;
import com.bs.bean.Bills;
import com.bs.service.BillTypeService;
import com.bs.service.BillsService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class BillsController {
    @Resource
    private BillsService billsService;
    @Resource
    private BillTypeService billTypeService;

    @RequestMapping("/getbills")
    public String getbills(ModelMap modelMap){
        PageInfo<Bills> billsPageInfo = billsService.getBills(1, -1, null, null);
        List<BillType> billTypes = billTypeService.getTypes();
        modelMap.addAttribute("billsPageInfo", billsPageInfo);
        modelMap.addAttribute("billTypes", billTypes);
        return "show";
    }

    @RequestMapping("/find")
    public String find(@RequestParam(defaultValue = "1") int index, @RequestParam(defaultValue = "-1") int typeId, String fromDate, String toDate, ModelMap modelMap){
        List<BillType> types = billTypeService.getTypes();
        PageInfo<Bills> billsPageInfo = billsService.getBills(index, typeId, fromDate, toDate);
        modelMap.addAttribute("billsPageInfo", billsPageInfo);
        modelMap.addAttribute("billTypes", types);
        modelMap.addAttribute("typeId", typeId);
        modelMap.addAttribute("fromDate", fromDate);
        modelMap.addAttribute("toDate", toDate);
        return "show";
    }

    @RequestMapping("/getTypes")
    public String getTypess(ModelMap modelMap){
        List<BillType> types = billTypeService.getTypes();
        modelMap.addAttribute("types", types);
        return "add";
    }

    @RequestMapping("/insert")
    public String insert(Bills bills){
        int result = billsService.insert(bills);
        if(result == 1){
            return "redirect:/getbills";
        }else{
            return "redirect:/getTypes";
        }
    }

    @RequestMapping("/findByBillId")
    public String findByBillId(Integer billid, ModelMap modelMap){
        Bills bill = billsService.selectByPrimaryKey(billid);
        List<BillType> types = billTypeService.getTypes();
        modelMap.addAttribute("bill", bill);
        modelMap.addAttribute("types", types);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Bills bills){
        int result = billsService.updateByPrimaryKey(bills);
        if(result == 1){
            return "redirect:/getbills";
        }
        return "redirect:/findByBillId";
    }

    @RequestMapping("/deleteById")
    public void deleteById(Integer billid, HttpServletResponse response){
        int result = billsService.deleteByPrimaryKey(billid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            if(result == 1){
                writer.print("<script>alert('删除成功');location.href='/getbills'</script>");
                return;
            }
            writer.print("<script>alert('删除失败');location.href='/getbills'</script>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
